use std::ffi::{CStr, CString, c_char};

unsafe extern "C" {
  pub static plugin_name: *const c_char;
  fn i18n_log(msg: *const c_char);
  fn i18n_err(msg: *const c_char);
}

#[static_init::dynamic]
pub static NAME: String = unsafe { CStr::from_ptr::<'static>(plugin_name) }.to_string_lossy().into_owned();

pub fn log(message: String) {
  unsafe {
    let string = format!("[{}] {}", NAME.as_str(), message);
    let cstring = CString::new(string).expect("failed to create CString");
    i18n_log(cstring.as_c_str().as_ptr());
  }
}

pub fn err(message: String) {
  unsafe {
    let string = format!("[{}] {}", NAME.as_str(), message);
    let cstring = CString::new(string).expect("failed to create CString");
    i18n_err(cstring.as_c_str().as_ptr());
  }
}
